﻿using System;
using System.Collections.Generic;
using System.Linq;
using FrontEnd.Application.MembershipFacade;
using FrontEnd.Models;

namespace UnitTests
{
    internal class StubMembershipFacade : IMembershipFacade
    {
        private readonly List<AccountCortege> accounts = new List<AccountCortege>();

        private class AccountCortege
        {
            public AccountInfo Account 
            {
                get;
                set;
            }

            public string Password
            {
                get;
                set;
            }
        }

        private AccountCortege GetCortegeByEmail(string email)
        {
            return accounts.First(a => a.Account.Person.EmailAddress == email);
        }

        public string GetPasswordByEmail(string email)
        {
            return GetCortegeByEmail(email).Password;
        }

        public AccountInfo GetAccountByEmail(string email)
        {
            return GetCortegeByEmail(email).Account;
        }

        public RegistrationResult Register(string email, string displayName, string password)
        {

            AccountInfo account =
                new AccountInfo
                {
                    Id = email,
                    Person =
                        new PersonInfo
                        {
                            EmailAddress = email,
                            DisplayName = displayName
                        }
                };

            this.accounts.Add(new AccountCortege { Account = account, Password = password });

            return new RegistrationResult {Person = account.Person, IsSuccess = true};
        }

        public AuthenticationResult Authenticate(string email, string password)
        {
            return new AuthenticationResult{ IsSuccess = GetCortegeByEmail(email).Password == password };
        }

        public void AddAccount(AccountInfo account, string password)
        {
            this.accounts.Add(new AccountCortege{Account = account, Password = password});
        }
    }
}
